package com.jdrx.ocp.service;

import com.jdrx.ocp.beans.entity.UserServiceClickPO;
import com.jdrx.ocp.dao.UserServiceClickDAO;
import com.jdrx.platform.commons.rest.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Huangxg
 * @Date: 2018/11/26 20:04
 */
@Service
public class UserServiceClickService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceClickService.class);

    @Autowired
    UserServiceClickDAO userServiceClickDAO;

    public Integer doClick(Long userId,String prefix) throws BizException {
        try{
            Map<String,Object> map = new HashMap<>(4);
            map.put("userId",userId);
            map.put("prefix",prefix);
            UserServiceClickPO userServiceClickPO = userServiceClickDAO.getByUserIdAndPrefix(map);
            if(userServiceClickPO == null){
                //新增点击记录
                userServiceClickPO = new UserServiceClickPO();
                userServiceClickPO.setUserId(userId);
                userServiceClickPO.setPrefix(prefix);
                userServiceClickPO.setNumber(1);
                return userServiceClickDAO.insert(userServiceClickPO);
            }else{
                //更新点击次数
                userServiceClickPO.setNumber(userServiceClickPO.getNumber() + 1);
                return userServiceClickDAO.update(userServiceClickPO);
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new BizException("操作数据失败！");
        }
    }

}
